﻿namespace Service.Framework.Utils.Extension
{
    public static class TimeExtension
    {



        /// <summary>
        /// 时间转Cron表达式
        /// </summary>
        /// <param name="time"></param>
        /// <returns></returns>
        public static string DatetimeToCron(this DateTime time)
        {
            var year = time.Year;
            var month = time.Month;
            var day = time.Day;
            var hour = time.Hour;
            var minute = time.Minute;
            string cron = $"0 {minute} {hour} {day} {month} ? {year}";
            return cron;
        }



        /// <summary>
        /// 13位长度时间戳转为C#格式时间    
        /// </summary>
        /// <param name="timeStamp"></param>
        /// <returns></returns>
        public static DateTime ToDateTime13(Int64 timeStamp)
        {
            //System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1)); // 当地时区
            System.DateTime startTime = new System.DateTime(1970, 1, 1); // 当地时区

            return startTime.AddTicks(timeStamp * 10000);
        }

        /// <summary>        
        /// 10位长度时间戳时间戳转为C#格式时间        
        /// </summary>        
        /// <param name="timeStamp">10位长度时间戳</param>        
        /// <returns></returns>        
        public static DateTime ToDateTime10(Int64 timeStamp)
        {
            //System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1)); // 当地时区
            System.DateTime startTime = new System.DateTime(1970, 1, 1); // 

            return startTime.AddTicks(timeStamp * 10000000);
        }

        /// <summary>
        /// 将传入的时间转化为10位长度的时间戳(秒)
        /// </summary>
        /// <param name="time"></param>
        /// <returns></returns>
        public static Int64 ToTimestamp10(this DateTime time)
        {
            return (time.ToUniversalTime().Ticks - (1970L * 365L * 24L * 60L * 60L * 1000L * 10000L)) / 10000000;
        }

        /// <summary>
        /// 将传入的时间转化为13位长度的时间戳（毫秒）
        /// </summary>
        /// <param name="date"></param>
        /// <returns></returns>
        public static long ToTimestamp13(this DateTime date)
        {
            return (date.ToUniversalTime().Ticks - (1970L * 365L * 24L * 60L * 60L * 1000L * 10000L)) / 10000;
        }
    }
}
